﻿using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Win32;

namespace Toenda.Foundation.SystemLayer {
	/// <summary>
	/// Class WindowsRegistry
	/// </summary>
	public class WindowsRegistry {
		/// <summary>
		/// Read a key from the registry
		/// </summary>
		/// <param name="start"></param>
		/// <param name="path"></param>
		/// <param name="subKey"></param>
		/// <returns></returns>
		/// <exception cref="ArgumentNullException">If parameter start or path is null.</exception>
		public static Dictionary<string, string> ReadKeyValues(KeyStartPoint start, string path) {
			if(start == null) {
				throw new ArgumentNullException("The parameter start cannot be null.");
			}

			if(path == null) {
				throw new ArgumentNullException("The parameter path cannot be null.");
			}

			RegistryKey regKey;

			if(path.StartsWith("/") || path.StartsWith("\\")) {
				path = path.Substring(1);
			}

			switch(start) {
				case KeyStartPoint.ClassesRoot:
					regKey = Registry.ClassesRoot.OpenSubKey(path);
					break;

				case KeyStartPoint.CurrentConfig:
					regKey = Registry.CurrentConfig.OpenSubKey(path);
					break;

				case KeyStartPoint.CurrentUser:
					regKey = Registry.CurrentUser.OpenSubKey(path);
					break;

				case KeyStartPoint.DynData:
					regKey = Registry.DynData.OpenSubKey(path);
					break;

				case KeyStartPoint.LocalMachine:
					regKey = Registry.LocalMachine.OpenSubKey(path);
					break;

				case KeyStartPoint.PerformanceData:
					regKey = Registry.PerformanceData.OpenSubKey(path);
					break;

				case KeyStartPoint.Users:
					regKey = Registry.Users.OpenSubKey(path);
					break;

				default:
					regKey = Registry.LocalMachine.OpenSubKey(path);
					break;
			}

			if(regKey != null) {
				//return regKey.GetValue(subKey).ToString();

				Dictionary<string, string> dict = new Dictionary<string, string>();

				string[] keyNames = regKey.GetSubKeyNames();

				foreach(string key in keyNames) {
					RegistryKey subRegKey = regKey.OpenSubKey(key);

					try {
						dict.Add(
							key,
							subRegKey.GetValue("").ToString()
						);
					}
					catch(Exception) {
					}
				}

				return dict;
			}
			else {
				return new Dictionary<string, string>();
			}
		}

		/// <summary>
		/// Read a key from the registry
		/// </summary>
		/// <param name="start"></param>
		/// <param name="path"></param>
		/// <param name="subKey"></param>
		/// <returns></returns>
		/// <exception cref="ArgumentNullException">If parameter start, path or subKeys is null.</exception>
		public static string ReadKey(KeyStartPoint start, string path, string subKey) {
			if(start == null) {
				throw new ArgumentNullException("The parameter start cannot be null.");
			}

			if(path == null) {
				throw new ArgumentNullException("The parameter path cannot be null.");
			}

			if(subKey == null) {
				throw new ArgumentNullException("The parameter subKey cannot be null.");
			}

			RegistryKey regKey;

			if(path.StartsWith("/") || path.StartsWith("\\")) {
				path = path.Substring(1);
			}

			switch(start) {
				case KeyStartPoint.ClassesRoot:
					regKey = Registry.ClassesRoot.OpenSubKey(path);
					break;

				case KeyStartPoint.CurrentConfig:
					regKey = Registry.CurrentConfig.OpenSubKey(path);
					break;

				case KeyStartPoint.CurrentUser:
					regKey = Registry.CurrentUser.OpenSubKey(path);
					break;

				case KeyStartPoint.DynData:
					regKey = Registry.DynData.OpenSubKey(path);
					break;

				case KeyStartPoint.LocalMachine:
					regKey = Registry.LocalMachine.OpenSubKey(path);
					break;

				case KeyStartPoint.PerformanceData:
					regKey = Registry.PerformanceData.OpenSubKey(path);
					break;

				case KeyStartPoint.Users:
					regKey = Registry.Users.OpenSubKey(path);
					break;

				default:
					regKey = Registry.LocalMachine.OpenSubKey(path);
					break;
			}

			if(regKey != null) {
				return regKey.GetValue(subKey).ToString();
			}
			else {
				return "";
			}
		}
	}
}
